define(['common', 'toastr', 'bootstrapTable', 'bootstrapTableZh', 'icheck'], function(common, toastr) {
    var userList = [];
    return {
        init: function() {
            $("#userTableData").bootstrapTable({
                method: 'post',
                silent: true,
                url: common.ip + "userMng?" + localStorage.loginUid,
                pageSize: 10,
                pageNumber: 1,
                sidePagination: "server",
                queryParamsType: '',
                pagination: true,
                paginationLoop: true,
                striped: true,
                search: true,
                responseHandler: function(res) {
                    common.heartbeat();
                    var tableData = { total: 0, rows: [] };
                    if (res.code === -4) {
                        common.login();
                    } else if (res.length > 0) {
                        res.forEach(function(item, index) {
                            item.option = ['<label class="radio-inline">',
                                '    <input type="radio" class="icheckInput" name="code-' + item.id + '" value="1" ' + (item.code == 1 ? "checked" : "") + '> 是',
                                '</label>',
                                '<label class="radio-inline">',
                                '    <input type="radio" class="icheckInput" name="code-' + item.id + '" value="0" ' + (item.code == 0 ? "checked" : "") + '> 否',
                                '</label>'
                            ].join("");
                        });
                        tableData.total = res.length;
                        tableData.rows = res;
                    }
                    userList = tableData.rows;
                    return tableData;
                }
            }).on('load-success.bs.table', function() {
                $('.icheckInput').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'icheckbox_square-green'
                }).on('ifChecked', function() {
                    console.log(this);
                    common.handleAjax('userMng', { id: $(this).attr('name').split('-')[1], code: $(this).val() }, true, true).done(function(result) {
                        if (result.code === -1) {
                            toastr.error('更改失败，请重试。');
                            return;
                        }
                        if (result.code === -4) {
                            return;
                        }
                        if (result !== 1) {
                            toastr.error('更改失败，请重试。');
                        }
                    });
                });
            });
        },
        colStyle: function(value, row, index) {
            return {
                css: {
                    padding: 0
                }
            }
        },
        imgFormatter: function(value, row, index) {
            return '<img src="' + (value ? value : "../../img/user.jpg") + '" class="img-circle img-responsive">';
        }
    }
});